package org.sam.swing.docking.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;

import org.apache.commons.lang3.StringUtils;
import org.sam.swing.JSPanel;
import org.sam.swing.JSPanelToolBar;
import org.sam.swing.interpreter.Enginee;
import org.sam.swing.interpreter.NashornEnginee;
import org.sam.swing.resource.ResourceLoader;

/**
 * 用来动态执行脚本的窗口
 * 
 * @author sam
 *
 */
public class JSPanelShell extends JSPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * 构造函数
	 */
	public JSPanelShell() {
		this.initCompents();
	}

	/**
	 * 显示结果的文本框
	 */
	private JTextPane logWindow;

	/**
	 * 输入的指令脚本
	 */
	private JTextField txtInput;

	/**
	 * 执行脚本按钮
	 */
	private JButton btnRun;

	/**
	 * 顶部工具栏
	 */
	private JSPanelToolBar toolbar;

	/**
	 * 当前的脚本解释引擎
	 */
	private Enginee enginee;

	/**
	 * 当前的脚本解释引擎
	 * 
	 * @return
	 */
	public Enginee getEnginee() {
		if (this.enginee == null) {
			this.enginee = new NashornEnginee();
		}
		return enginee;
	}

	/**
	 * 当前的脚本解释引擎
	 * 
	 * @param enginee
	 */
	public void setEnginee(Enginee enginee) {
		this.enginee = enginee;
	}

	/**
	 * 初始化控件显示
	 */
	private void initCompents() {
		this.setLayout(new BorderLayout());
		JScrollPane scrollPane = new JScrollPane();
		logWindow = new JTextPane();
		scrollPane.setViewportView(logWindow);

		logWindow.setFont(new Font("微软雅黑", Font.TRUETYPE_FONT, 14));
		// logTextArea.setForeground(Color.RED);
		logWindow.setForeground(Color.BLACK);
		logWindow.setEditable(false);
		logWindow.setAutoscrolls(true);

		toolbar = new JSPanelToolBar();
		JButton btnClear = toolbar.buildButton(ResourceLoader.IMAGE_CANCEL10, "", "清空日志", (e) -> {
			logWindow.setText("");
		});
		// 帮助文件
		JButton btnHelp = toolbar.buildButton(ResourceLoader.IMAGE_QUESTION10, "", "帮助", (e) -> {
			try {
				Object help = this.getEnginee().callFun("help");
				if (help != null) {
					logWindow.setText(help.toString());
				}
			} catch (Exception ex) {
				try {
					logWindow.getDocument().insertString(logWindow.getDocument().getLength(), ex.getMessage() + "\r\n", null);
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
			}
		});

		toolbar.add(btnClear);
		toolbar.add(btnHelp);

		this.add(toolbar, BorderLayout.NORTH);
		this.add(scrollPane, BorderLayout.CENTER);

		// 拼接底部工具栏
		ScriptAction action = new ScriptAction();
		txtInput = new JTextField();
		btnRun = new JButton(action);

		JPanel panelInput = new JPanel();
		panelInput.setLayout(new BorderLayout());

		panelInput.add(txtInput, BorderLayout.CENTER);
		panelInput.add(btnRun, BorderLayout.EAST);

		this.add(panelInput, BorderLayout.SOUTH);

		txtInput.addActionListener(action);
		KeyStroke keys = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
		txtInput.registerKeyboardAction(action, keys, WHEN_FOCUSED);
	}

	/**
	 * 执行脚本的action对象
	 * 
	 * @author sam
	 *
	 */
	private class ScriptAction extends AbstractAction {

		private static final long serialVersionUID = 1L;

		/**
		 * 初始化数据
		 */
		private ScriptAction() {
			this.putValue(AbstractAction.NAME, "");
			this.putValue(AbstractAction.SMALL_ICON,
					new ImageIcon(ResourceLoader.getResource(ResourceLoader.IMAGE_PLAY)));
			this.putValue(SHORT_DESCRIPTION, "执行脚本");
		}

		/**
		 * 执行操作
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			String text = txtInput.getText();
			if (StringUtils.isEmpty(text))
				return;

			try {
				String txt = txtInput.getText();
				Object value = getEnginee().eval(txt);
				if (value != null) {
					logWindow.getDocument().insertString(logWindow.getDocument().getLength(), value.toString() + "\r\n", null);
				} else {
					logWindow.getDocument().insertString(logWindow.getDocument().getLength(), txt + "执行完成..\r\n", null);
				}
				txtInput.setText("");

			} catch (Exception ex) {
				try {
					logWindow.getDocument().insertString(logWindow.getDocument().getLength(), ex.getMessage() + "\r\n", null);
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
